package com.jiangdg.demo

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Bitmap
import android.view.View
import android.view.animation.Animation
import android.widget.ImageView
import android.widget.RelativeLayout
import com.bumptech.glide.Glide
import razerdp.basepopup.BasePopupWindow

class ImagePopup(context: Context, private val bitmap: Bitmap) : BasePopupWindow(context) {

    init {
        setContentView(R.layout.popup_image)
        findViewById<RelativeLayout>(R.id.rlClose).setOnClickListener {
            dismiss()
        }
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        val imageView: ImageView = findViewById(R.id.image_view_popup)
//        Glide.with(context).load(R.mipmap.ic_logo).into(imageView)
        imageView.setImageBitmap(bitmap)
    }
    override fun onCreateShowAnimator(): Animator {
        return createScaleAnimation(true)
    }

    override fun onCreateDismissAnimator(): Animator {
        return createScaleAnimation(false)
    }

    private fun createScaleAnimation(show: Boolean): Animator {
        val startScale = if (show) 0.5f else 1f
        val endScale = if (show) 1f else 0.5f

        val scaleX = ObjectAnimator.ofFloat(contentView, "scaleX", startScale, endScale)
        val scaleY = ObjectAnimator.ofFloat(contentView, "scaleY", startScale, endScale)

        return AnimatorSet().apply {
            playTogether(scaleX, scaleY)
            duration = 300
        }
    }

}
